<?php
/**
 * Created by PhpStorm.
 * User: Ysll <754060604@qq.com>
 * Date: 2017/9/29
 * Time: 17:29
 */

namespace Common\Model;


use Think\Model;

class PermissionRoles extends Model
{
    protected $tableName = 'permission_roles';
    public $role_id;
    public $permissions_id;
    public $item = array();

    public function __construct($role_id)
    {
        parent::__construct();
        $this->role_id = $role_id;
        $this->init();
    }

    private function init()
    {
        $result = $this->where('roles_id = ' . $this->role_id)->select();
        foreach ($result as $item) {
            $this->item[] = new Permissions($item['permissions_id']);
        }
    }

    public static function deleteByRoleId($role_id)
    {
        return M('permission_roles')->where('roles_id = ' . $role_id)->delete();
    }

    public static function creates($role_id, array $permissions = null) {
        foreach ($permissions as $permissions_id) {
            $p = new Permissions($permissions_id);
            if ($p->isQuery) {
                M('permission_roles')->add([
                    'roles_id' => $role_id,
                    'permissions_id' => $permissions_id
                ]);
            }
        }
    }
}